package scct.zhang.dao;

import scct.zhang.model.ChatRecord;
import scct.zhang.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ChatRecordDao {
    public ChatRecord login(int conversationid, String sendername, String receivername, String msgtype, String content, String sendtime) {
        ChatRecord chatRecord = null;
//    获取数据库连接对象
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_chatrecord where conversationid=?,sendername=?receivername=?,msgtype=?,content=?,sendtime=?";
//    预编译查询对象
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);

            pstmt.setInt(1,conversationid);
            pstmt.setString(2,sendername);
            pstmt.setString(3, receivername);
            pstmt.setString(4,msgtype);
            pstmt.setString(5,content);
            pstmt.setString(6,sendtime);

//        查询
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                int conversationid2 = rs.getInt("conversationid");
                String sendername2 = rs.getString("sendername");
                String receivername2 = rs.getString("receivername");
                String msgtype2 = rs.getString("msgtype");
                String content2 = rs.getString("content");
                String sendtime2 = rs.getString("sendtime");
                chatRecord = new ChatRecord(id,conversationid2,sendername2,receivername2,msgtype2,content2,sendtime2);
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return chatRecord;
    }

    /**
     * @param conversationid
     * @param sendername
     * @param receivername
     * @param msgtype
     * @param content
     * @param sendtime
     * @return 用户对象
     */

//    添加联系人
    public boolean addConversation(int conversationid, String sendername, String receivername, String msgtype, String content, String sendtime) {
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String sql = "insert into sm_chatrecord values(null,?,?,?,?,?,?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(6,sendtime);
            pstmt.setString(5,content);
            pstmt.setString(4,msgtype);
            pstmt.setString(3,receivername);
            pstmt.setString(2,sendername);
            pstmt.setInt(1,conversationid);
            result = pstmt.executeUpdate();
        } catch (SQLException e){
            throw new RuntimeException(e);
        }
        return result == 1;
    }

    public boolean isDupname(int id){
        int count = 0;// 查到数量
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_chatrecord where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                count =1;
            }
//            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count>0;
    }


    public List<ChatRecord> getAllChatRecord() {
        Connection conn = DBUtils.getConnection();
        List<ChatRecord> chatRecords = new ArrayList<>();

        String sql = "select * from sm_chatrecord";
        try {
            PreparedStatement pstme = conn.prepareStatement(sql);
            ResultSet rs = pstme.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                int conversationid = rs.getInt("conversationid");
                String sendername =rs.getString("sendername");
                String receivername = rs.getString("receivername");
                String msgtype = rs.getString("msgtype");
                String content = rs.getString("content");
                String sendtime = rs.getString("sendtime");
                chatRecords.add(new ChatRecord(id,conversationid,sendername,receivername,msgtype,content,sendtime));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return chatRecords;
    }

    public ChatRecord getChatRecordId(int id){
        ChatRecord chatRecord = null;
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_chatrecord where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);

            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                int conversationid = rs.getInt("conversationid");
                String sendername =rs.getString("sendername");
                String receivername = rs.getString("receivername");
                String msgtype = rs.getString("msgtype");
                String content = rs.getString("content");
                String sendtime = rs.getString("sendtime");
                chatRecord = new ChatRecord(id,conversationid,sendername,receivername,msgtype,content,sendtime);
            }
            DBUtils.closeConnection(conn);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return chatRecord;
    }

    public boolean deleteChatRecord(int id) {
        int num = 0;
        Connection conn = DBUtils.getConnection();
        String sqlStr = "delete from sm_chatrecord where id = ?";
        try{
            PreparedStatement pstmt = conn.prepareStatement(sqlStr);
            pstmt.setInt(1,id);
            num = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return num > 0;
    }

    public boolean updateChatRecord(int id,int conversationid, String sendername, String receivername, String msgtype, String content, String sendtime){
        int count = 0;
        Connection conn = DBUtils.getConnection();
        String update_sql = "update sm_chatrecord set conversationid=?,sendername=?,receivername=?,msgtype=?,content=?,sendtime=? where id = ?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(update_sql);
            pstmt.setString(6,sendtime);
            pstmt.setString(5,content);
            pstmt.setString(4,msgtype);
            pstmt.setString(3,receivername);
            pstmt.setString(2,sendername);
            pstmt.setInt(1,conversationid);
            pstmt.setInt(7,id);
            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count > 0;
    }

//    public ArrayList<ChatRecord> getChatRecordByName(String name) {
//        ArrayList<ChatRecord> chatRecords = new ArrayList<>();
//        Connection conn = DBUtils.getConnection();
//        String sql = "select * from sm_chatrecord where name like '%" + name + "%'";
//        try {
//            PreparedStatement pstmt = conn.prepareStatement(sql);
//            ResultSet rs = pstmt.executeQuery();
//            while (rs.next()) {
//                int id = rs.getInt("id");
//                int conversationid = rs.getInt("conversationid");
//                String sendername =rs.getString("sendername");
//                String receivername = rs.getString("receivername");
//                String msgtype = rs.getString("msgtype");
//                String content = rs.getString("content");
//                String sendtime = rs.getString("sendtime");
//                chatRecords.add(new ChatRecord(id,conversationid,sendername,receivername,msgtype,content,sendtime));
//            }
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        return chatRecords;
//    }

}
